import { defineDirective } from "@/shared/define.js";

export const CompileLinkDynamicForm = defineDirective(
  /**
   * @param {ng.ICompileService} $compile
   * @returns {ng.IDirective}
   */
  ($compile) => ({
    restrict: "A",
    scope: {
      dynamicForm: "=",
      $ctrl: "<ctrl",
      $event: "&",
    },
    compile(element, attrs) {
      return function link(scope, element, attrs) {
        scope.$watch("dynamicForm", (newValue) => {
          if (newValue) {
            // 编译动态加载的 HTML
            const compiled = $compile(newValue)(scope);
            console.log("compiled", compiled);
            // 清空元素内容并插入编译后的 HTML
            element.empty().append(compiled);
          } else {
            element.empty();
          }
        });
      };
    },

    // /**
    //  * @param {ng.WIScope} scope
    //  */
    // link(scope, element, attrs) {
    //   console.log("CompileLinkDynamicForm", scope, attrs);
    //   scope.$watch("dynamicForm", function (newValue) {
    //     if (newValue) {
    //       // 编译动态加载的 HTML
    //       var compiled = $compile(newValue)(scope);
    //       console.log("compiled", compiled);
    //       // 清空元素内容并插入编译后的 HTML
    //       element.empty().append(compiled);
    //     }else {
    //       element.empty();
    //     }
    //   });
    // },
  }),
  ["$compile"],
);
